<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>WD_Chao 智能车控制</title>
</head>
<body>
<div id="sse">
    <a href="javascript:connect_web()">连接服务</a>
    <input type="text" id="web_cmd" value="$4WD,1,0,0,0,0,0,0,0#"/>
    <button onclick="javascript:exc_cmd_input()">执行命令</button>
    <div>
        -----
        <button onclick="javascript:exc_cmd('$4WD.RUN.1.0#')">上</button>
        -----
        <p>
            <button onclick="javascript:exc_cmd('$4WD.RUN.3.0#')">左</button>
            <button onclick="javascript:exc_cmd('$4WD.CMD.STOP#')">停止</button>
            <button onclick="javascript:exc_cmd('$4WD.RUN.4.0#')">右</button>
        </p>
        -----
        <button onclick="javascript:exc_cmd('$4WD.RUN.2.0#')">下</button>
        -----
    </div>
    <div>
        -----
        <button onclick="javascript:exc_cmd('$4WD,PTZ.3.001#')">上</button>
        -----
        <p>
            <button onclick="javascript:exc_cmd('$4WD,PTZ.2.001#')">左</button>
            <button onclick="javascript:exc_cmd('$4WD.PTZ.0.000#')">停止</button>
            <button onclick="javascript:exc_cmd('$4WD,PTZ.2.002#')">右</button>
        </p>
        -----
        <button onclick="javascript:exc_cmd('$4WD,PTZ.3.002#')">下</button>
        -----
    </div>
</div>
</body>
<script src="jquery-3.3.1.min.js"></script>
<script type="text/javascript">
    var ws;

    function connect_socket() {
        // 打开一个 web socket
        ws = new WebSocket("ws://www.bzchao.com:19900/auth=admin");
        ws.onopen = function () {
            // Web Socket 已连接上，使用 send() 方法发送数据
            console.log("链接已建立，数据发送中...");
            ws.send("客户端测试数据请求");
        };
        ws.onmessage = function (evt) {
            var received_msg = evt.data;
            alert("数据接收数据：" + received_msg);
        };
        ws.onclose = function () {
            // 关闭 websocket
            alert("连接已关闭...");
        };
    }

    connect_socket();

    function connect_web() {
        if ("WebSocket" in window) {
            connect_socket()
        } else {
            // 浏览器不支持 WebSocket
            alert("您的浏览器不支持 WebSocket!");
        }
    }

    function exc_cmd_input() {
        let cmdText = $("#web_cmd").val();
        ws.send(cmdText);
    }

    function exc_cmd(str) {
        ws.send(str);
    }
</script>
</html>